//
//  ThanksViewController.m
//  LocomotiveGuards
//
//  Created by admin on 15/12/14.
//  Copyright (c) 2015年 REDOOR. All rights reserved.
//

#import "ThanksViewController.h"
#import "PopMsg.h"
#import "PopLoadMsg.h"

@interface ThanksViewController ()
{
    UILabel *lab_adress;
    UILabel *lab_phone;
    UILabel *lab_man;
    UIButton *btn_zan;
    UILabel *lab_count;
    NSString *psbname;
    NSString *count;
    NSString *psbdistrict;
    NSString *psbadress;
    NSString *province;
    NSString *city;
    BOOL start;
    BOOL end;
}

@end

@implementation ThanksViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.rightbtn removeFromSuperview];
    [self.centerbtn setImage:nil forState:UIControlStateNormal];
    [self.centerbtn setTitle:@"   答谢" forState:UIControlStateNormal];
    //self.centerbtn.titleLabel.textAlignment=NSTextAlignmentCenter;
    self.view.backgroundColor = [UIColor whiteColor];
    [self.leftbtn addTarget:self action:@selector(goBackButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    //self.tabBarController.delegate=self;
    [[NSUserDefaults standardUserDefaults]setObject:@"0" forKey:@"yiwan"];
    province=@"";
    city=@"";
    psbname=@"XXX";
    count=@"0";
    psbdistrict=@"";
    psbname=@"";
    [self configView];

}

-(void)configView{
    UIImageView *showview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 20+44*H, SCREEN_WIDTH, 180*H)];
    showview.image=[UIImage imageNamed:@"1pic_bg"];
    [self.view addSubview:showview];
    UILabel *lab1=[[UILabel alloc]initWithFrame:CGRectMake(40*W, 20+44*H+185*H, SCREEN_WIDTH-50*W, 20*H)];
    lab1.text=@"您的车辆已被找回";
    lab1.font=[UIFont systemFontOfSize:17];
    lab1.textColor=[UIColor colorWithRed:(CGFloat)40/255 green:(CGFloat)152/255 blue:(CGFloat)250/255 alpha:1.0];
    [self.view addSubview:lab1];
    
    UILabel *lab2=[[UILabel alloc]initWithFrame:CGRectMake(40*W, 20+44*H+210*H, SCREEN_WIDTH-50*W, 20*H)];
    lab2.text=@"请及时前往派出所领取";
    lab2.font=[UIFont systemFontOfSize:17];
    lab2.textColor=[UIColor colorWithRed:(CGFloat)40/255 green:(CGFloat)152/255 blue:(CGFloat)250/255 alpha:1.0];
    [self.view addSubview:lab2];
    
    lab_adress=[[UILabel alloc]initWithFrame:CGRectMake(42*W, 20+44*H+240*H, SCREEN_WIDTH-60*W, 20*H)];
    lab_adress.text=@"地址:";
    lab_adress.font=[UIFont systemFontOfSize:15];
    lab_adress.textColor=[UIColor grayColor];
    [self.view addSubview:lab_adress];
    
    lab_phone=[[UILabel alloc]initWithFrame:CGRectMake(42*W, 20+44*H+260*H, SCREEN_WIDTH-60*W, 20*H)];
    lab_phone.text=@"电话:";
    lab_phone.font=[UIFont systemFontOfSize:15];
    lab_phone.textColor=[UIColor grayColor];
    [self.view addSubview:lab_phone];
    
    lab_man=[[UILabel alloc]initWithFrame:CGRectMake(42*W, 20+44*H+280*H, SCREEN_WIDTH-60*W, 20*H)];
    lab_man.text=@"联系人:";
    lab_man.font=[UIFont systemFontOfSize:15];
    lab_man.textColor=[UIColor grayColor];
    [self.view addSubview:lab_man];
    
    UILabel *lab3=[[UILabel alloc]initWithFrame:CGRectMake(40*W, 20+44*H+310*H, SCREEN_WIDTH-50*W, 20*H)];
    lab3.text=@"车辆已经找回了";
    lab3.font=[UIFont systemFontOfSize:17];
    lab3.textColor=[UIColor colorWithRed:(CGFloat)40/255 green:(CGFloat)152/255 blue:(CGFloat)250/255 alpha:1.0];
    [self.view addSubview:lab3];
    
    UILabel *lab4=[[UILabel alloc]initWithFrame:CGRectMake(40*W, 20+44*H+335*H, SCREEN_WIDTH-50*W, 20*H)];
    lab4.text=@"请为辛勤工作的";
    lab4.font=[UIFont systemFontOfSize:17];
    lab4.textColor=[UIColor colorWithRed:(CGFloat)40/255 green:(CGFloat)152/255 blue:(CGFloat)250/255 alpha:1.0];
    [self.view addSubview:lab4];
    
    UILabel *lab5=[[UILabel alloc]initWithFrame:CGRectMake(40*W, 20+44*H+360*H, SCREEN_WIDTH-50*W, 20*H)];
    lab5.text=@"人民警察同志点个赞吧！";
    lab5.font=[UIFont systemFontOfSize:17];
    lab5.textColor=[UIColor colorWithRed:(CGFloat)40/255 green:(CGFloat)152/255 blue:(CGFloat)250/255 alpha:1.0];
    [self.view addSubview:lab5];

    btn_zan=[[UIButton alloc]initWithFrame:CGRectMake(45*W, 20+44*H+390*H, 100*W, 35*H)];
    [btn_zan setImage:[UIImage imageNamed:@"11pic_btn01"] forState:UIControlStateNormal];
    [btn_zan setImage:[UIImage imageNamed:@"11pic_btn01_on"] forState:UIControlStateHighlighted];
    [btn_zan addTarget:self action:@selector(tappad_zan) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn_zan];
    
    lab_count=[[UILabel alloc]initWithFrame:CGRectMake(160*W, 20+44*H+385*H, 100*W, 45*H)];
    lab_count.text=@"某某派出所已获XXX个赞";
    lab_count.textColor=[UIColor grayColor];
    lab_count.numberOfLines=0;
    lab_count.font=[UIFont systemFontOfSize:14];
    [self.view addSubview:lab_count];
    [self getEbicycleInfo];
    
}

-(void)successPostUpvote{
    
    NSInteger count1=count.integerValue+1;
    count=[NSString stringWithFormat:@"%li",count1];
    lab_count.text=[NSString stringWithFormat:@"%@已获%@个赞",psbname,count];
    //[self goBackButtonClicked];
}

#pragma mark - 网络请求

//发送赞
-(void)postUpVote{
    start=YES;
    end=YES;
    PopLoadMsg *loadmsg=[PopLoadMsg new];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        sleep(startloadtime);
        dispatch_async(dispatch_get_main_queue(), ^{
            if(start==YES){
                [loadmsg loadImages];
            }
        });
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        sleep(startloadtime+endloadtime);
        dispatch_async(dispatch_get_main_queue(), ^{
            end=NO;
            if(start==NO){
                [loadmsg.imageview removeFromSuperview];
            }
        });
    });

    NSString * urlMethod = [NSString stringWithFormat:@"%@postUpvote.do",urlstring];
    NSDictionary * paramDict = @{@"district":psbdistrict,@"psbName":psbname,@"province":province,@"city":city,@"comment":@"",@"loginId":[[NSUserDefaults standardUserDefaults]objectForKey:@"phonenumber"],@"bid":[[NSUserDefaults standardUserDefaults]objectForKey:@"bid"]};
    [K_AppDelegate GenerateHTTPRequestWithUrl:urlMethod withParam:paramDict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary * responseDict = responseObject;
        NSNumber * flag = [responseDict objectForKey:@"flag"];
        end=NO;
        if(start==NO){
            [loadmsg.imageview removeFromSuperview];
        }

        if (flag.longValue == 0 ){
            [PopMsg showMessage:@"点赞成功" Lasttime:1.0f];
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"感谢您的赞赏" message:@"您的鼓励使我们前进的动力!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            
            [self successPostUpvote];
        }
        else{
            NSString *msg=[responseDict objectForKey:@"msg"];
            if(msg){
                NSString *message=[msg stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                [PopMsg showMessage:message Lasttime:1.0f];
            }
        }
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"查询防护状态失败！");
        start=NO;
        [PopMsg showMessage:@"网络出错" Lasttime:1.0f];
        if(end==NO){
            [loadmsg.imageview removeFromSuperview];
        }

    }];
}
//根据派出所获取点赞总数

-(void)getcount_zan{
    
    start=YES;
    end=YES;
    PopLoadMsg *loadmsg=[PopLoadMsg new];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        sleep(startloadtime);
        dispatch_async(dispatch_get_main_queue(), ^{
            if(start==YES){
                [loadmsg loadImages];
            }
        });
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        sleep(startloadtime+endloadtime);
        dispatch_async(dispatch_get_main_queue(), ^{
            end=NO;
            if(start==NO){
                [loadmsg.imageview removeFromSuperview];
            }
        });
    });

    NSString * urlMethod = [NSString stringWithFormat:@"%@getUpvoteNum.do",urlstring];
    NSDictionary * paramDict = @{@"district":psbdistrict,@"psbName":psbname};
    [K_AppDelegate GenerateHTTPRequestWithUrl:urlMethod withParam:paramDict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        start=NO;
        if(end==NO){
            [loadmsg.imageview removeFromSuperview];
        }
        NSDictionary * responseDict = responseObject;
        NSNumber * flag = [responseDict objectForKey:@"flag"];
        
        if (flag.longValue == 0 ){
            count=[responseDict objectForKey:@"count"];
            lab_count.text=[NSString stringWithFormat:@"%@已获%@个赞",psbname,count];
        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"查询防护状态失败！");
        [PopMsg showMessage:@"网络出错" Lasttime:1.0f];
        start=NO;
        if(end==NO){
            [loadmsg.imageview removeFromSuperview];
        }
    }];

}

- (void)getEbicycleInfo
{
    
    start=YES;
    end=YES;
    PopLoadMsg *loadmsg=[PopLoadMsg new];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        sleep(startloadtime);
        dispatch_async(dispatch_get_main_queue(), ^{
            if(start==YES){
                [loadmsg loadImages];
            }
        });
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        sleep(startloadtime+endloadtime);
        dispatch_async(dispatch_get_main_queue(), ^{
            end=NO;
            if(start==NO){
                [loadmsg.imageview removeFromSuperview];
            }
        });
    });
  
    NSString * urlMethod = [NSString stringWithFormat:@"%@getLostReport.do",urlstring];
    NSDictionary * paramDict = @{@"loginId":[[NSUserDefaults standardUserDefaults]objectForKey:@"phonenumber"],@"bid":[[NSUserDefaults standardUserDefaults]objectForKey:@"bid"]};
    
    [K_AppDelegate GenerateHTTPRequestWithUrl:urlMethod withParam: paramDict success:
     ^(AFHTTPRequestOperation *operation, id responseObject) {
         NSDictionary * responseDict = responseObject;
         start=NO;
         if(end==NO){
             [loadmsg.imageview removeFromSuperview];
         }
         
         NSNumber * flag = [responseDict objectForKey:@"flag"];
         if (flag.longValue == 0 )
         {
             province=[[responseDict objectForKey:@"province"]stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
             city=[[responseDict objectForKey:@"city"]stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
             
             NSString *str_adress=[responseDict objectForKey:@"psbAddress"];
             psbadress=[str_adress stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
             NSString *str_phone=[responseDict objectForKey:@"psbTelephone"];
             NSString *str_man=[[responseDict objectForKey:@"psbContact"]stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
             lab_adress.text=[NSString stringWithFormat:@"地址: %@",[[province stringByAppendingString:city]stringByAppendingString:psbadress]];
             
             lab_phone.text=[NSString stringWithFormat:@"电话: %@",str_phone];
             lab_man.text=[NSString stringWithFormat:@"联系人: %@",str_man];
             // lab_phone.text=
             psbdistrict=[[responseDict objectForKey:@"district"]stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
             
             psbname=[[responseDict objectForKey:@"psbName"]stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
             lab_count.text=[NSString stringWithFormat:@"%@已获%@个赞",psbname,count];
             [self getcount_zan];
             NSString *isUpvoted=[responseDict objectForKey:@"isUpvoted"];
             if(isUpvoted.integerValue==0){
                 //未点赞
                 btn_zan.enabled=YES;
                 [btn_zan setImage:[UIImage imageNamed:@"11pic_btn01"] forState:UIControlStateNormal];
                 [btn_zan setImage:[UIImage imageNamed:@"11pic_btn01_on"] forState:UIControlStateHighlighted];
             }
             else{
                 //已经点赞
                 btn_zan.enabled=NO;
                 [btn_zan setImage:[UIImage imageNamed:@"11pic_btn01_off"] forState:UIControlStateNormal];
             }
         }
         else
         {
             
         }
         
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"查询防护状态失败！");
         [PopMsg showMessage:@"网络出错" Lasttime:1.0f];
         start=NO;
         if(end==NO){
             [loadmsg.imageview removeFromSuperview];
         }

     }];
}

- (void)viewDidDisappear:(BOOL)animated{
    NSString *yiwan=[[NSUserDefaults standardUserDefaults]objectForKey:@"yiwan"];
    if([yiwan isEqualToString:@"1"]){
        [self.navigationController popToRootViewControllerAnimated:NO];
    }
}

#pragma mark - button clicled handler -

-(void)tappad_zan{
    NSLog(@"zan");
    [self postUpVote];
}

- (void)goBackButtonClicked
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(buttonIndex==0){
        [self goBackButtonClicked];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
